package com.mall.b2c.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.mall.common.CommonSetting.RoundType;
import com.mall.common.util.CommonSettingUtils;
import com.mall.common.util.EnumUtils;
import com.mall.common.util.PropertiesHelper;

public class DictionaryUtils {

	
	public static Map<String, String> getDictionaryMap(String prefix){
		Properties properties = PropertiesHelper.getPropertiesByPrefix("i18n_zh_CN", prefix);
		Map<String, String> retMap = new HashMap<String, String>();
		if(properties != null && properties.size() > 0){
			for(Object key : properties.keySet()){
				retMap.put(StringUtils.split((String)key, ".")[1], (String)properties.get(key));
			}
		}
		return retMap;
	}
	
	/**
	 * 根据枚举值获取对应的名称
	 * @param obj 枚举值
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static String getEnumName(Object obj){
		if(obj == null){
			return null;
		}
		Class clazz = obj.getClass();
		if(!clazz.isEnum()){
			return null;
		}
		return EnumUtils.enumProp2I18nString(clazz, obj.toString());
	}
	
	/**
	 * 根据父编码获取字典列表
	 * @param parentCode
	 * @return
	 */
	public static JSONArray getDictionary(String parentCode){
		return CommonSettingUtils.getDictionaryDataListForJSON(parentCode);
	}
	
	/**
	 * 获取字典的配置
	 * @param decode
	 * @return
	 */
	public static JSONObject getDictionaryConfig(String decode){
		return CommonSettingUtils.getDictionaryConfig(decode);
	}
	
	/**
	 * 获取字典的配置值Map
	 * @param key
	 * @return
	 */
	public static JSONObject getDictionaryValue(String key){
		return CommonSettingUtils.getDictionaryDataForJSON(key);
	}
	
	public static Map<String, String> getAllRoundType(){
		return EnumUtils.enumProp2I18nMap(RoundType.class);
	}
	
}
